import { http } from '@renderer/plugins/axios'
import { ElMessage } from 'element-plus'
import { ref } from 'vue'
import useUtil from './useUtil'

export default () => {
  const calculateList = ref<ApiPage<CalculateModel>>()
  const roomList = ref<RoomModel[]>()
  const costList = ref<CostModel>()
  const discountList = ref<DiscountModel[]>()
  const finishedList = ref<FinishedModel[]>()
  const costBalanceList = ref<CostListModel[]>()
  const MaterialList = ref<MaterialListModel[]>()
  const housesInfo = ref<HousesInfoModel[]>()
  const getCalculateList = useUtil().request(async (params) => {
    try {
      calculateList.value = await http.request({
        url: '/getCalculateList',
        method: 'GET',
        params,
      })
    } catch (error) {
      console.log(error)
    }
  })
  const getGatherCount = useUtil().request(async (params) => {
    try {
      const res = await http.request<HousesInfoModel[]>({
        url: '/getGatherCount',
        method: 'GET',
        params,
      })
      res.map((item, index) => {
        switch (index) {
          case 0:
            item.house_name = '一层数据采集'
            break
          case 1:
            item.house_name = '二层数据采集'
            break
          case 2:
            item.house_name = '三层数据采集'
            break
          case 3:
            item.house_name = '四层数据采集'
            break
          case 4:
            item.house_name = '五层数据采集'
            break
        }
      })
      housesInfo.value = res
    } catch (error) {
      console.log(error)
    }
  })
  const getTableDataType = useUtil().request(async (params) => {
    try {
      return await http.request({
        url: '/gui/getTableDataType',
        method: 'GET',
        params,
      })
    } catch (error) {
      return null
    }
  })
  const setSignBill = useUtil().request(async (data) => {
    try {
      await http.request({
        url: '/setSignBill',
        method: 'POST',
        data,
      })
      ElMessage({ type: 'success', message: '操作成功' })
    } catch (error) {
      console.log(error)
    }
  })
  const delCalculate = useUtil().request(async (data) => {
    try {
      await http.request({
        url: '/delCalculate',
        method: 'POST',
        data,
      })
      ElMessage({ type: 'success', message: '操作成功' })
    } catch (error) {
      console.log(error)
    }
  })
  const getCalculateTemplateList = useUtil().request(async (params: any) => {
    try {
      const res = await http.request<TemplateComputeModel[]>({
        url: '/getCalculateTemplateList',
        method: 'GET',
        params,
      })
      return res
    } catch (error) {
      return null
    }
  })
  const createCalculateGather = useUtil().request(async (data) => {
    try {
      await http.request({
        url: '/createCalculateGather',
        method: 'POST',
        data,
      })
      ElMessage({ type: 'success', message: '创建成功' })
    } catch (error) {
      console.log(error)
    }
  })
  const updateCalculateName = useUtil().request(async (data) => {
    try {
      await http.request({
        url: '/updateCalculateName',
        method: 'POST',
        data,
      })
      ElMessage({ type: 'success', message: '重命名成功' })
    } catch (error) {
      console.log(error)
    }
  })
  const copyGather = useUtil().request(async (data) => {
    try {
      await http.request({
        url: '/copyGather',
        method: 'POST',
        data,
      })
      ElMessage({ type: 'success', message: '复制成功' })
    } catch (error) {
      console.log(error)
    }
  })
  const getBudgetList = useUtil().request(async (params) => {
    try {
      roomList.value = await http.request<RoomModel[]>({
        url: '/getBudgetList',
        method: 'GET',
        params,
      })
    } catch (error) {
      console.log(error)
    }
  })
  const getDisCountList = useUtil().request(async (params) => {
    try {
      discountList.value = await http.request<DiscountModel[]>({
        url: '/getBudgetList',
        method: 'GET',
        params,
      })
    } catch (error) {
      console.log(error)
    }
  })
  const getFinishedList = useUtil().request(async (params) => {
    try {
      const list = await http.request<FinishedModel[]>({
        url: '/getBudgetList',
        method: 'GET',
        params,
      })
      list.map((item) => {
        item.remark = item.remark ? item.remark.replace(/#/g, '\n') : ''
      })
      finishedList.value = list
    } catch (error) {
      console.log(error)
    }
  })
  const getMainMaterialsList = useUtil().request(async (params) => {
    try {
      const res = await http.request<MainMaterialsModel[]>({
        url: '/getBudgetList',
        method: 'GET',
        params,
      })
      return res
    } catch (error) {
      return null
    }
  })
  const addBudgetData = useUtil().request(async (data) => {
    try {
      await http.request({
        url: '/addBudgetData',
        method: 'POST',
        data,
      })
      ElMessage({ type: 'success', message: '创建成功' })
    } catch (error) {
      console.log(error)
    }
  })
  const getCostList = useUtil().request(async (params) => {
    try {
      costList.value = await http.request<CostModel>({
        url: '/getCostList',
        method: 'GET',
        params,
      })
    } catch (error) {
      console.log(error)
    }
  })
  const getCostCateDetailRecord = useUtil().request(async (params) => {
    try {
      costBalanceList.value = await http.request<CostListModel[]>({
        url: '/getCostCateDetailRecord',
        method: 'GET',
        params,
      })
    } catch (error) {
      console.log(error)
    }
  })
  const addCostDetailData = useUtil().request(async (data) => {
    try {
      await http.request({
        url: '/addCostDetailData',
        method: 'POST',
        data,
      })
      ElMessage({ type: 'success', message: '创建成功' })
    } catch (error) {
      console.log(error)
    }
  })
  const getCalculateMaterialsInfo = useUtil().request(async (params) => {
    try {
      MaterialList.value = await http.request<MaterialListModel[]>({
        url: '/getCalculateMaterialsInfo',
        method: 'GET',
        params,
      })
    } catch (error) {
      console.log(error)
    }
  })
  const exportMaterialsTable = useUtil().request(async (data) => {
    try {
      const res = await http.request({
        url: '/gui/exportMaterialsTable',
        method: 'POST',
        data,
        responseType: 'blob',
      })
      return res
    } catch (error) {
      return null
    }
  })
  const exportBasicTable = useUtil().request(async (data) => {
    try {
      const res = await http.request({
        url: '/gui/exportBasicTable',
        method: 'POST',
        data,
        responseType: 'blob',
      })
      return res
    } catch (error) {
      return null
    }
  })
  const exportMainTable = useUtil().request(async (data) => {
    const res = await http.request({
      url: '/gui/exportMainTable',
      method: 'POST',
      data,
      responseType: 'blob',
    })
    return res
  })
  const exportWardrobeTable = useUtil().request(async (data) => {
    try {
      const res = await http.request({
        url: '/gui/exportWardrobeTable',
        method: 'POST',
        data,
        responseType: 'blob',
      })
      return res
    } catch (error) {
      return null
    }
  })
  const exportFavorableTable = useUtil().request(async (data) => {
    const res = await http.request({
      url: '/gui/exportFavorableTable',
      method: 'POST',
      data,
      responseType: 'blob',
    })
    return res
  })
  const exportBasic2Table = useUtil().request(async (data) => {
    try {
      const res = await http.request({
        url: '/gui/exportBasic2Table',
        method: 'POST',
        data,
        responseType: 'blob',
      })
      return res
    } catch (error) {
      return null
    }
  })
  const exportBasic3Table = useUtil().request(async (data) => {
    try {
      const res = await http.request({
        url: '/gui/exportBasic3Table',
        method: 'POST',
        data,
        responseType: 'blob',
      })
      return res
    } catch (error) {
      return null
    }
  })
  const exportSettleTable = useUtil().request(async (data) => {
    try {
      const res = await http.request({
        url: '/gui/exportSettleTable',
        method: 'POST',
        data,
        responseType: 'blob',
      })
      return res
    } catch (error) {
      return null
    }
  })
  const getHouseRecord = useUtil().request(async (data) => {
    try {
      const res = await http.request({
        url: '/getHouseRecord',
        method: 'GET',
        params: data,
      })
      return res
    } catch (error) {
      return null
    }
  })
  const saveGatherData = useUtil().request(async (data) => {
    try {
      await http.request({
        url: '/gui/saveGatherData',
        method: 'POST',
        data,
      })
      ElMessage({ type: 'success', message: '保存成功' })
    } catch (error) {
      console.log(error)
    }
  })
  const resetInter = useUtil().request(async (data) => {
    try {
      await http.request({
        url: '/resetInter',
        method: 'POST',
        data,
      })
    } catch (error) {
      console.log(error)
    }
  })
  return {
    getCalculateList,
    calculateList,
    setSignBill,
    getCalculateTemplateList,
    createCalculateGather,
    getBudgetList,
    roomList,
    addBudgetData,
    costList,
    getCostList,
    discountList,
    getDisCountList,
    finishedList,
    getFinishedList,
    getMainMaterialsList,
    getCostCateDetailRecord,
    costBalanceList,
    addCostDetailData,
    getCalculateMaterialsInfo,
    MaterialList,
    exportMaterialsTable,
    exportSettleTable,
    exportFavorableTable,
    exportWardrobeTable,
    exportMainTable,
    exportBasicTable,
    exportBasic2Table,
    saveGatherData,
    resetInter,
    updateCalculateName,
    copyGather,
    getHouseRecord,
    getTableDataType,
    delCalculate,
    getGatherCount,
    housesInfo,
    exportBasic3Table,
  }
}
